package com.ptmall.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.OrderDetilBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.LikeGoodAdapter;
import com.ptmall.app.ui.adapter.OrderGoodAdapter;
import com.ptmall.app.utils.TimeUtil;
import com.ptmall.app.view.NoScrollGridView;
import com.ptmall.app.view.NoScrollListView;
import com.ptmall.app.view.TitleBarView;

import java.util.HashMap;
import java.util.Map;

public class OrderDetilActivity extends BaseMvpActivity {

    TitleBarView titlebar;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    String id;
    ImageView paystatus_img;

    NoScrollGridView likelist;
    NoScrollListView goodlist;

    LikeGoodAdapter mLikeGoodAdapter;
    OrderGoodAdapter mOrderGoodAdapter;

    TextView name, phone, address, order_status, orderprice, yunfeiprice, youhuiprice, totalprice, orderno, createtime, paytime, fahuotime, chengjiaotime;
    Button btn1, btn2;

    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_orderdetil);
    }

    @Override
    public void initViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        paystatus_img = (ImageView) findViewById(R.id.paystatus_img);
        orderno = (TextView) findViewById(R.id.orderno);
        fahuotime = (TextView) findViewById(R.id.fahuotime);
        chengjiaotime = (TextView) findViewById(R.id.chengjiaotime);
        createtime = (TextView) findViewById(R.id.createtime);
        paytime = (TextView) findViewById(R.id.paytime);
        orderprice = (TextView) findViewById(R.id.orderprice);
        order_status = (TextView) findViewById(R.id.order_status);
        titlebar = (TitleBarView) findViewById(R.id.titlebar);
        name = (TextView) findViewById(R.id.name);
        phone = (TextView) findViewById(R.id.phone);
        address = (TextView) findViewById(R.id.address);
        goodlist = (NoScrollListView) findViewById(R.id.goodlist);
        likelist = (NoScrollGridView) findViewById(R.id.likelist);
        yunfeiprice = (TextView) findViewById(R.id.yunfeiprice);
        orderprice = (TextView) findViewById(R.id.orderprice);
        youhuiprice = (TextView) findViewById(R.id.youhuiprice);
        totalprice = (TextView) findViewById(R.id.totalprice);
        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

    }

    @Override
    public void initEvents() {
        id = getIntent().getStringExtra("id");
        mLikeGoodAdapter = new LikeGoodAdapter(getContext(), null);
        likelist.setAdapter(mLikeGoodAdapter);
        mOrderGoodAdapter = new OrderGoodAdapter(getContext(), null);
        goodlist.setAdapter(mOrderGoodAdapter);
        mLikeGoodAdapter.setClickInterface(new LikeGoodAdapter.ClickInterface() {
            @Override
            public void shopcaradd(ImageView iv, int position, String id) {
                addcar(id);

            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void addcar(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("productId", id);
        map.put("num", "1");
        apiDataRepository.addShopCar(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("加入购物车成功");
            }
        });
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.getOrderDetil(map, new ApiNetResponse<OrderDetilBean>(getContext()) {
            @Override
            public void onSuccess(OrderDetilBean data) {
                orderno.setText("订单编号：" + data.order_number);
                createtime.setText("创建时间：" + TimeUtil.getStrTime(data.create_time));
                mLikeGoodAdapter.pushData(data.like);
                name.setText(data.address.trueName);
                phone.setText(data.address.tel);
                address.setText(data.address.area + data.address.address);
                mOrderGoodAdapter.pushData(data.product_list);

                yunfeiprice.setText("￥" + data.freight);
                orderprice.setText("￥" + data.orders_total);
                youhuiprice.setText("￥" + data.youhui);
                totalprice.setText("￥" + data.orders_total);
                try {

                    switch (data.order_status) {
                        case "1":
                            order_status.setText("等待买家付款");
                            paytime.setVisibility(View.GONE);
                            fahuotime.setVisibility(View.GONE);
                            chengjiaotime.setVisibility(View.GONE);
                            paystatus_img.setImageResource(R.drawable.ddzf);
                            break;
                        case "2":
                            order_status.setText("待配送");
                            paytime.setVisibility(View.VISIBLE);
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paystatus_img.setImageResource(R.drawable.dps);
                            fahuotime.setVisibility(View.GONE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                        case "3":
                        case "21":
                            order_status.setText("到达小区配送中");
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paytime.setVisibility(View.VISIBLE);
                            paystatus_img.setImageResource(R.drawable.psz);
                            fahuotime.setText("发货时间：" + TimeUtil.getStrTime(data.song_time));
                            fahuotime.setVisibility(View.VISIBLE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                        case "30":
                            order_status.setText("小区配送中失败");
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paytime.setVisibility(View.VISIBLE);
                            paystatus_img.setImageResource(R.drawable.psz);
                            fahuotime.setText("发货时间：" + TimeUtil.getStrTime(data.song_time));
                            fahuotime.setVisibility(View.VISIBLE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                        case "4":
                            order_status.setText("已送达");
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paytime.setVisibility(View.VISIBLE);
                            paystatus_img.setImageResource(R.drawable.jycg);
                            fahuotime.setText("发货时间：" + TimeUtil.getStrTime(data.song_time));
                            fahuotime.setVisibility(View.VISIBLE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                        case "5":
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paytime.setVisibility(View.VISIBLE);
                            paystatus_img.setImageResource(R.drawable.jycg);
                            order_status.setText("交易成功");
                            fahuotime.setText("发货时间：" + TimeUtil.getStrTime(data.song_time));
                            fahuotime.setVisibility(View.VISIBLE);
                            chengjiaotime.setText("成交时间：" + TimeUtil.getStrTime(data.take_time));
                            chengjiaotime.setVisibility(View.VISIBLE);
                            break;
                        case "7":

                            order_status.setText("退货退款中");
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paytime.setVisibility(View.VISIBLE);
                            paystatus_img.setImageResource(R.drawable.dps);
                            fahuotime.setVisibility(View.GONE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                        case "6":
                            paytime.setVisibility(View.GONE);
                            paystatus_img.setImageResource(R.drawable.thsh);
                            order_status.setText("已取消");
                            fahuotime.setVisibility(View.GONE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                        case "11":
                            order_status.setText("拣货中");
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paytime.setVisibility(View.VISIBLE);
                            paystatus_img.setImageResource(R.drawable.dps);
                            fahuotime.setVisibility(View.GONE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                        case "12":
                            order_status.setText("待配送");
                            paytime.setText("付款时间：" + TimeUtil.getStrTime(data.pay_time));
                            paytime.setVisibility(View.VISIBLE);
                            paystatus_img.setImageResource(R.drawable.dps);
                            fahuotime.setVisibility(View.GONE);
                            chengjiaotime.setVisibility(View.GONE);
                            break;
                    }
                } catch (Exception e) {

                }

                try {
//                    switch (data.order_status) {
//                        case "1":
//                            btn2.setText("付款");
//                            btn2.setVisibility(View.VISIBLE);
//                            btn1.setText("取消订单");
//                            btn1.setVisibility(View.VISIBLE);
//                            break;
//                        case "2":
//                            btn1.setText("申请退款");
//                            btn1.setVisibility(View.VISIBLE);
//                            btn2.setVisibility(View.GONE);
//                            break;
//                        case "3":
//                        case "21":
//                        case "30":
//                            btn1.setText("申请退款");
//                            btn1.setVisibility(View.VISIBLE);
//                            btn2.setText("确认收货");
//                            btn2.setVisibility(View.VISIBLE);
//                            break;
//                        case "4":
//                            btn2.setText("确认收货");
//                            btn2.setVisibility(View.VISIBLE);
//                            break;
//                        case "5":
//                            btn1.setText("申请退款");
//                            btn1.setVisibility(View.VISIBLE);
//                            btn2.setText("删除订单");
//                            btn2.setVisibility(View.VISIBLE);
//                            break;
//                        case "6":
//                            break;
//                        case "7":
//                            switch (data.refund_status) {
//                                case "1":
//                                    btn1.setText("取消退款");
//                                    btn1.setVisibility(View.VISIBLE);
//                                    break;
//                                case "2":
//
//                                    btn1.setText("取消退款");
//                                    btn1.setVisibility(View.VISIBLE);
//                                    break;
//                                case "3":
//
//                                    btn1.setText("删除订单");
//                                    btn1.setVisibility(View.VISIBLE);
//                                    break;
//                                case "4":
//
//                                    btn1.setText("删除订单");
//                                    btn1.setVisibility(View.VISIBLE);
//                                    break;
//                            }
//                            break;
//                        case "11":
//                            //拣货中
//                            btn1.setText("申请退款");
//                            btn1.setVisibility(View.VISIBLE);
//                            btn2.setVisibility(View.GONE);
//                            break;
//                        case "12":
//                            //待配送
//                            btn1.setText("申请退货");
//                            btn1.setVisibility(View.VISIBLE);
//                            btn2.setVisibility(View.GONE);
//                            break;
//                    }
//                    btn1.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            switch (data.order_status) {
//                                case "1":
//                                    //取消订单
//                                    orderCancel(id);
//                                    break;
//                                case "2":
//
//                                    intent = new Intent(getContext(), ThActivity.class);
//                                    intent.putExtra("id", id);
//                                    intent.putExtra("price", data.orders_total);
//                                    startActivity(intent);
//                                    break;
//                                case "3":
//                                case "21":
//                                case "30":
//                                case "4":
//
//                                    intent = new Intent(getContext(), ThActivity.class);
//                                    intent.putExtra("id", id);
//                                    intent.putExtra("price", data.orders_total);
//                                    startActivity(intent);
//                                    break;
//                                case "5":
//                                    //删除订单
////                                    orderDel(id);
//
//                                    intent = new Intent(getContext(), ThActivity.class);
//                                    intent.putExtra("id", id);
//                                    intent.putExtra("price", data.orders_total);
//                                    startActivity(intent);
//                                    break;
//                                case "11":
//                                case "12":
//                                    intent = new Intent(getContext(), ThActivity.class);
//                                    intent.putExtra("id", id);
//                                    intent.putExtra("price", data.orders_total);
//                                    startActivity(intent);
//                                    break;
//                                case "7":
//                                    switch (data.refund_status) {
//                                        case "1":
//                                            qxtk(data.orderId);
////                                            btn1.setText("取消退款");
////                                            btn1.setVisibility(View.VISIBLE);
//                                            break;
//                                        case "2":
//
//                                            qxtk(data.orderId);
////                                            btn1.setText("取消退款");
////                                            btn1.setVisibility(View.VISIBLE);
//                                            break;
//                                        case "3":
//                                            orderDel(data.orderId);
////                                            btn1.setText("删除订单");
////                                            btn1.setVisibility(View.VISIBLE);
//                                            break;
//                                        case "4":
//
//                                            orderDel(data.orderId);
////                                            btn1.setText("删除订单");
////                                            btn1.setVisibility(View.VISIBLE);
//                                            break;
//                                    }
//                                    break;
//
//                            }
//                        }
//                    });
//                    btn2.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            switch (data.order_status) {
//                                case "1":
//                                    //付款
//                                    orderpay(id, data.orders_total);
//                                    break;
//                                case "2":
//                                    //确认收货
//                                    conSh(id);
//                                    break;
//                                case "3":
//                                case "21":
//                                case "30":
//                                case "4":
//                                    conSh(id);
//                                    break;
//                                case "5":
//                                    orderDel(id);
//                                    break;
//                                case "11":
//
//                                    break;
//                            }
//                        }
//                    });
                } catch (Exception e) {

                }
            }
        });
    }


    private void orderCancel(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.orderCancel(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("取消成功");
                finish();
            }
        });
    }

    private void orderDel(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.orderDel(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("删除成功");
                finish();
            }
        });
    }

    private void conSh(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", id);
        apiDataRepository.conSh(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("确认收货成功");
                getData();
            }
        });
    }

    private void qxtk(String id){
        Map<String,String> map = new HashMap<>();
        map.put("orderId",id);
        apiDataRepository.qxtk(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("取消成功");
                finish();
            }
        });
    }
    private void orderpay(String id, String price) {
        Intent intent = new Intent(getContext(), PayActivity.class);
        intent.putExtra("orderId", id);
        intent.putExtra("orders_total", price);
        startActivity(intent);
    }
}
